package client.gui.interfaces.component;

import java.awt.Graphics;

@SuppressWarnings("serial")
public class Container extends Component {

	public enum Layout {
		ABSOLUTE, HORIZONTAL, VERTICAL;
	}

	private Layout layout = Layout.HORIZONTAL;

	public Container(int id, int x, int y, int width, int height) {
		super(id, x, y, width, height);
	}
	
	public Container(int id, int width, int height) {
		super(id, width, height);
	}

	public void addChild(Component child) {
		child.parent = this;
		this.children.add(child);
	}

	public void positionChildren() {
		int unit, top, left, childrenVisible = 0;
		int amount = 1;

		for (Component child : this.children) {
			if (child.getVisible()) {
				childrenVisible++;
			}
		}
		if (layout == Layout.HORIZONTAL) {
			for (Component child : this.children) {
				if (!child.getVisible())
					continue;
				unit = this.width / (childrenVisible * 2);
				top = (this.height / 2) - (child.height / 2);
				left = (unit * amount) - (child.width / 2);
				child.setLocation(left, top);
				amount += 2;
			}
		} else if (layout == Layout.VERTICAL) {
			for (Component child : this.children) {
				if (!child.getVisible())
					continue;
				unit = this.height / (childrenVisible * 2);
				left = (this.width / 2) - (child.width / 2);
				top = (unit * amount) - (child.height / 2);
				child.setLocation(left, top);
				amount += 2;
			}
		}
	}

	@Override
	void drawComponent(Graphics g) {}
	
	public void setLayout(Layout layout) {
		this.layout = layout;
	}

	public Layout getLayout() {
		return layout;
	}
	
}
